<?php

namespace app\admin\model;

use app\common\model\TimeModel;

/**
 * @property int $id ID 
 * @property \app\admin\model\User $user 用户信息 
 * @property \app\admin\model\UserLevel $userLevel 会员身份 
 * @property string $order_sn 订单编号 
 * @property float|double $total_money 金额 
 * @property int $pay_status 支付状态 1:已支付,0:未支付
 * @property int $pay_time 支付时间 
 * @property int $create_time 创建时间 
 */
class OrderVip extends TimeModel
{

    protected $name = "order_vip";

    protected $deleteTime = false;

    
    public const SELECT_LIST_PAY_STATUS = ['1'=>'已支付','0'=>'未支付',];

    
    
    public function user()
    {
        return $this->belongsTo('\app\admin\model\User', 'user_id', 'id');
    }

    public function userLevel()
    {
        return $this->belongsTo('\app\admin\model\UserLevel', 'level_id', 'id');
    }

    public static function createVipOrder($data,$user)
    {
        $user_level = \app\admin\model\UserLevel::where('id',$data['level_id'])->find();

        if(!$user_level){
            exception('会员等级不存在');
        }
        $money = $user_level['price'];
        $orderData['user_id'] = $user['id'];
        $orderData['order_sn'] = get_order_sn('VIP');
        $orderData['total_money'] = $money;
        $orderData['level_id'] = $data['level_id'];
        //插入订单充值表
        $order = self::create($orderData);
        
        //插入订单主表
        \app\admin\model\Order::create([
            'user_id' => $user['id'],
            'order_type' => 2,
            'order_sn' => $orderData['order_sn'],
            'order_money' => $money,
            'payable_money' => $money,
        ]);
        return [
            'order_sn' => $orderData['order_sn'],
            'money' => $money
        ];
    }

}